﻿using OpenIddict.Abstractions;
using OpenIddict.Server;
using System.Threading.Tasks;
using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Server.OpenIddictServerEvents;

namespace Wuther.Infrastructure.ExtensionGrantTypes
{
    public class CaptchaValidator : IOpenIddictServerHandler<HandleTokenRequestContext>
    {
        public async ValueTask HandleAsync(HandleTokenRequestContext context)
        {
            if (context.Request.IsPasswordGrantType())
            {
                // 获取扩展参数
                var sessionId = context.Request["session_id"]?.ToString();
                var captchaCode = context.Request["captcha_code"]?.ToString();

                if (string.IsNullOrEmpty(sessionId) || string.IsNullOrEmpty(captchaCode))
                {
                    context.Reject(
                        error: Errors.InvalidRequest,
                        description: "必须提供验证码参数");
                    return;
                }

                //if (!await _captchaService.ValidateCaptchaAsync(sessionId, captchaCode))
                //{
                //    context.Reject(
                //        error: Errors.InvalidRequest,
                //        description: "验证码错误");
                //    return;
                //}
            }
        }
    }
}
